package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.R;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ImageGroup extends RelativeLayout implements View.OnClickListener, View.OnLongClickListener {

    private static final int MAX_COUNT = 6;

    private DisplayImageOptions options; //配置图片加载及显示选项

    public interface OnClickImageListener {
        void onClickImage(int index);

        void onLongClickImage(int index);
    }

    private OnClickImageListener mOnClickImageListener;

    private RoundedImageView mIvArray[];

    public ImageGroup(Context context) {
        super(context);
        init(context);
    }

    public ImageGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ImageGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.timeline_image1:
                onClickImage(0);
                break;
            case R.id.timeline_image2:
                onClickImage(1);
                break;
            case R.id.timeline_image3:
                onClickImage(2);
                break;
            case R.id.timeline_image4:
                onClickImage(3);
                break;
            case R.id.timeline_image5:
                onClickImage(4);
                break;
            case R.id.timeline_image6:
                onClickImage(5);
                break;
        }
    }

    @Override
    public boolean onLongClick(View v) {
        switch (v.getId()) {
            case R.id.timeline_image1:
                onLongClickImage(0);
                return true;
            case R.id.timeline_image2:
                onLongClickImage(1);
                return true;
            case R.id.timeline_image3:
                onLongClickImage(2);
                return true;
            case R.id.timeline_image4:
                onLongClickImage(3);
                return true;
            case R.id.timeline_image5:
                onLongClickImage(4);
                return true;
            case R.id.timeline_image6:
                onLongClickImage(5);
                return true;
        }
        return false;
    }

    public void setOnClickImageListener(OnClickImageListener onClickImageListener) {
        mOnClickImageListener = onClickImageListener;
    }

    public void setImageArray(String... urls) {
        for (int i = 0; i < MAX_COUNT; i++) {
            mIvArray[i].setImageDrawable(null);
            mIvArray[i].setVisibility(View.GONE);
        }
        if (urls.length > MAX_COUNT) {
            return;
        }
        for (int i=0;i<urls.length;i++){
            ImageLoader.getInstance().displayImage("file://" + urls[i], mIvArray[i], options, null);
            mIvArray[i].setVisibility(View.VISIBLE);
        }
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_image_group, this);
        mIvArray = new RoundedImageView[6];
        mIvArray[0] = (RoundedImageView) findViewById(R.id.timeline_image1);
        mIvArray[1] = (RoundedImageView) findViewById(R.id.timeline_image2);
        mIvArray[2] = (RoundedImageView) findViewById(R.id.timeline_image3);
        mIvArray[3] = (RoundedImageView) findViewById(R.id.timeline_image4);
        mIvArray[4] = (RoundedImageView) findViewById(R.id.timeline_image5);
        mIvArray[5] = (RoundedImageView) findViewById(R.id.timeline_image6);
        for (int i = 0; i < MAX_COUNT; i++) {
            mIvArray[i].setOnClickListener(this);
            mIvArray[i].setOnLongClickListener(this);
        }
        setOptions();
    }

    private void onClickImage(int index) {
        if (mOnClickImageListener != null) {
            mOnClickImageListener.onClickImage(index);
        }
    }

    private void onLongClickImage(int index) {
        if (mOnClickImageListener != null) {
            mOnClickImageListener.onLongClickImage(index);
        }
    }

    // 配置图片加载及显示选项
    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_photo_empty)    //在ImageView加载过程中显示图片
                .showImageForEmptyUri(R.drawable.ic_photo_empty)  //image连接地址为空时
                .showImageOnFail(R.drawable.ic_photo_empty)  //image加载失败
                // .cacheInMemory(true)  //加载图片时会在内存中加载缓存
                // .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }
}
